﻿package com.vicentediaz.view.imageselector
{
    import com.greensock.*;
    import com.greensock.easing.*;
    import com.myp.flow.*;
    import com.myp.mystage.*;
    import com.vicentediaz.data.*;
    import com.vicentediaz.flow.*;
    import com.vicentediaz.view.events.*;
    import com.vicentediaz.view.imageviewer.*;
    import flash.display.*;
    import flash.events.*;

    public class ImageSelector extends Sprite
    {
        private var container:Sprite;
        private var timeline:TimelineMax;
        private var imageViewer:ImageViewer;
        private var gallery:Gallery;
        private var slider:ImageSelectorSlider;
        private var markerContainer:Sprite;
        private var thumbsMask:Sprite;
        private var sliderMask:Sprite;
        private const CLOSE_HEIGHT:uint = 25;
        private var blackBase:Sprite;
        public var status:String;
        private var marker:Sprite;
        private const OPEN_HEIGHT:uint = 90;
        private static var instance:ImageSelector;
        public static const STATUS_OPEN:String = "open";
        public static const STATUS_CLOSED:String = "closed";

        public function ImageSelector()
        {
            this.status = STATUS_CLOSED;
            this.gallery = Gallery.getInstance();
            this.imageViewer = ImageViewer.getInstance();
            addEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage, false, 0, true);
            this.imageViewer.addEventListener(ImageViewerEvent.IMAGE_START_LOADING, this.onImageStartLoading, false, 0, true);
            this.imageViewer.addEventListener(ImageViewerEvent.IMAGE_LOADING_PROGRESS, this.onImageLoadingProgress, false, 0, true);
            this.imageViewer.addEventListener(ImageViewerEvent.IMAGE_LOADED, this.onImageLoaded, false, 0, true);
            this.imageViewer.addEventListener(ImageViewerEvent.IMAGE_CANCEL_LOAD, this.onImageCancelLoad, false, 0, true);
            instance = this;
            return;
        }// end function

        private function buildThumbs() : void
        {
            var _loc_2:Thumb;
            this.timeline = new TimelineMax();
            this.timeline.addLabel("closed", this.timeline.duration);
            var _loc_1:uint;
            while (_loc_1++ < this.gallery.totalThumbs)
            {
                // label
                _loc_2 = new Thumb();
                _loc_2.createFromBitmap(this.gallery.getThumbBitmapData(_loc_1));
                _loc_2.x = _loc_1 * (_loc_2.width + 2);
                this.container.addChild(_loc_2);
                _loc_2.addEventListener(MouseEvent.CLICK, this.onThumbMouseDown, false, 0, true);
                _loc_2.id = _loc_1;
                this.timeline.insert(TweenMax.from(_loc_2, 0.15, {y:_loc_2.height--, ease:Quad.easeOut}), 0);
            }// end while
            this.timeline.insert(TweenMax.from(this.sliderMask, 0.15, {height:1, y:1}), 0);
            this.timeline.insert(TweenMax.from(this.blackBase, 0.15, {height:this.CLOSE_HEIGHT}), 0);
            this.timeline.addLabel("open", this.timeline.duration);
            this.timeline.pause();
            return;
        }// end function

        private function onAreaMouseOver(param1:MouseEvent) : void
        {
            if (param1.target != param1.currentTarget)
            {
                return;
            }// end if
            this.open();
            return;
        }// end function

        private function open() : void
        {
            if (this.status == STATUS_OPEN)
            {
                return;
            }// end if
            this.timeline.tweenTo("open");
            this.status = STATUS_OPEN;
            Sounds.getInstance().playById("PAPER", 0.05, 0);
            return;
        }// end function

        private function onResize(param1:Event) : void
        {
            this.slider.width = MyStage.getInstance().width;
            this.slideFromValue();
            this.sliderMask.width = this.slider.width;
            this.drawBlackBase();
            this.drawThumbsMask();
            return;
        }// end function

        private function drawBlackBase() : void
        {
            this.blackBase.graphics.clear();
            this.blackBase.graphics.beginFill(0, 1);
            this.blackBase.graphics.drawRect(0, 0, MyStage.getInstance().width, -this.OPEN_HEIGHT);
            this.blackBase.graphics.endFill();
            return;
        }// end function

        private function onImageLoadingProgress(param1:ImageViewerEvent) : void
        {
            var _loc_2:* = this.container.getChildAt(param1.id) as Thumb;
            _loc_2.progress = param1.progress;
            return;
        }// end function

        private function onStageMouseUp(param1:MouseEvent) : void
        {
            if (!contains(param1.target as DisplayObject))
            {
                this.slider.scrubbing = false;
                this.close();
            }// end if
            return;
        }// end function

        private function drawThumbsMask() : void
        {
            this.thumbsMask.graphics.clear();
            this.thumbsMask.graphics.beginFill(255, 1);
            this.thumbsMask.graphics.drawRect(0, 0, MyStage.getInstance().width, 64);
            this.thumbsMask.graphics.endFill();
            return;
        }// end function

        private function onAreaMouseOut(param1:MouseEvent) : void
        {
            if (param1.target != param1.currentTarget)
            {
                return;
            }// end if
            this.close();
            return;
        }// end function

        private function onImageStartLoading(param1:ImageViewerEvent) : void
        {
            var _loc_2:Thumb;
            var _loc_3:uint;
            while (_loc_3++ < this.container.numChildren)
            {
                // label
                _loc_2 = Thumb(this.container.getChildAt(_loc_3));
                _loc_2.progress = 0;
                _loc_2.selected = _loc_2.id == ImageViewer.getInstance().currentImageId;
            }// end while
            this.imageViewer.addEventListener(ImageViewerEvent.IMAGE_LOADING_PROGRESS, this.onImageLoadingProgress, false, 0, true);
            return;
        }// end function

        private function drawSliderMask() : void
        {
            this.sliderMask.graphics.clear();
            this.sliderMask.graphics.beginFill(255, 1);
            this.sliderMask.graphics.drawRect(0, 0, MyStage.getInstance().width, 7);
            this.sliderMask.graphics.endFill();
            return;
        }// end function

        private function onSliderChange(param1:Event) : void
        {
            this.slideFromValue();
            return;
        }// end function

        private function drawMarker() : void
        {
            this.marker.graphics.clear();
            this.marker.graphics.beginFill(10066329, 1);
            this.marker.graphics.drawRect(0, 0, 64, 1);
            this.marker.graphics.endFill();
            return;
        }// end function

        private function onAddedToStage(param1:Event) : void
        {
            this.container = new Sprite();
            this.blackBase = new Sprite();
            this.sliderMask = new Sprite();
            this.thumbsMask = new Sprite();
            this.marker = new Sprite();
            this.markerContainer = new Sprite();
            this.slider = new ImageSelectorSlider();
            this.blackBase.y = 15;
            this.drawBlackBase();
            this.drawSliderMask();
            this.drawThumbsMask();
            this.drawMarker();
            addChild(this.blackBase);
            addChild(this.slider);
            this.markerContainer.addChild(this.marker);
            addChild(this.markerContainer);
            addChild(this.container);
            addChild(this.sliderMask);
            addChild(this.thumbsMask);
            this.slider.mask = this.sliderMask;
            this.container.mask = this.thumbsMask;
            this.buildThumbs();
            this.container.y = -67;
            this.thumbsMask.y = -67;
            this.marker.y = -7;
            this.slider.addEventListener(Event.CHANGE, this.onSliderChange, false, 0, true);
            MyStage.getInstance().stage.addEventListener(Event.RESIZE, this.onResize, false, 0, true);
            removeEventListener(Event.ADDED_TO_STAGE, this.onAddedToStage);
            addEventListener(MouseEvent.ROLL_OVER, this.onAreaMouseOver, false, 0, true);
            addEventListener(MouseEvent.ROLL_OUT, this.onAreaMouseOut, false, 0, true);
            stage.addEventListener(MouseEvent.MOUSE_UP, this.onStageMouseUp, false, 0, true);
            this.onResize(null);
            return;
        }// end function

        private function onImageCancelLoad(param1:ImageViewerEvent) : void
        {
            return;
        }// end function

        private function onImageLoaded(param1:ImageViewerEvent) : void
        {
            var _loc_3:Number;
            var _loc_2:* = this.container.getChildAt(param1.id) as Thumb;
            _loc_2.progress = 0;
            TweenMax.to(this.marker, this.status == STATUS_CLOSED ? (0.8) : (0), {x:_loc_2.x, ease:Quad.easeInOut});
            if (this.status == STATUS_CLOSED)
            {
                _loc_3 = 65 * param1.id / (this.container.width - MyStage.getInstance().width);
                TweenMax.to(this.slider, 0.8, {value:_loc_3, ease:Quad.easeInOut});
            }// end if
            return;
        }// end function

        private function slideFromValue() : void
        {
            this.container.x = (-(this.container.width - MyStage.getInstance().width)) * this.slider.value;
            this.markerContainer.x = this.container.x;
            return;
        }// end function

        private function onThumbMouseDown(param1:MouseEvent) : void
        {
            if (Flow.level(MainFlow.LEVEL).currentSection != MainFlow.IMAGEN)
            {
                Flow.level(MainFlow.LEVEL).request(MainFlow.IMAGEN, "ImageSelector");
            }// end if
            Flow.level(ImageFlow.LEVEL).request(Thumb(param1.currentTarget).id.toString(), "ImageSelector");
            Sounds.getInstance().playById("CLICK_IMAGE", 1, (-(stage.stageWidth / 2 - mouseX)) / stage.stageWidth);
            return;
        }// end function

        private function close() : void
        {
            if (this.status == STATUS_CLOSED)
            {
                return;
            }// end if
            if (this.slider.scrubbing)
            {
                return;
            }// end if
            this.timeline.tweenTo("closed");
            this.status = STATUS_CLOSED;
            Sounds.getInstance().playById("PAPER", 0.05, 0);
            return;
        }// end function

        public static function getInstance() : ImageSelector
        {
            return instance;
        }// end function

    }
}
